package com.weixiao.controller;

import com.alibaba.csp.sentinel.util.StringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.weixiao.enums.YesOrNoEnum;
import com.weixiao.handler.YyghException;
import com.weixiao.model.hosp.HospitalSet;
import com.weixiao.result.Result;
import com.weixiao.service.HospitalSetService;
import com.weixiao.utils.MD5;
import com.weixiao.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * author 韩诚
 * date 2022/9/20 10:58
 */
//@CrossOrigin 解决跨域问题，使用getway网关之后需要注释掉
@Api(tags = "医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;

    /**
     * 获取所有医院设置信息列表
     *
     * @return
     */
    @ApiOperation(value = "获取所有医院设置信息列表")
    @GetMapping("findAll")
    public Result findAllHospitalSet() {
        //调用service的方法
        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok(list);
    }

    /**
     * 删除医院设置信息
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "删除医院设置信息")
    @DeleteMapping("{id}")
    public Result removeHospSet(@PathVariable Long id) {
        boolean flag = hospitalSetService.removeById(id);
        if (flag) {
            return Result.ok();
        }
        return Result.fail();
    }

    /**
     * 获取医院设置信息分页列表
     *
     * @param current
     * @param limit
     * @param hospitalSetQueryVo
     * @return
     */
    @ApiOperation(value = "获取医院设置信息分页列表")
    @PostMapping("findPageHospSet/{current}/{limit}")
    public Result findPageHospSet(@PathVariable long current,
                                  @PathVariable long limit,
                                  @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo) {
        //创建page对象，传递当前页，每页记录数
        Page<HospitalSet> page = new Page<>(current, limit);
        String hosname = hospitalSetQueryVo.getHosname();//医院名称
        String hoscode = hospitalSetQueryVo.getHoscode();//医院编号
        //构建条件
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<HospitalSet>()
                .like(StringUtils.isNotBlank(hosname),"hosname", hosname)
                .eq(StringUtils.isNotBlank(hoscode),"hoscode", hoscode);

        //调用方法实现分页查询
        Page<HospitalSet> pageHospitalSet = hospitalSetService.page(page, wrapper);
        //返回结果
        return Result.ok(pageHospitalSet);
    }

    /**
     * 添加医院设置信息
     *
     * @param hospitalSet
     * @return
     */
    @ApiOperation(value = "添加医院设置信息")
    @PostMapping("saveHospitalSet")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet) {
        //设置状态 为使用
        hospitalSet.setStatus(YesOrNoEnum.YES.getCode());

        //签名秘钥
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + new Random().nextInt(1000)));

        //调用service
        boolean save = hospitalSetService.save(hospitalSet);
        if (save) {
            return Result.ok();
        }
        return Result.fail();
    }

    /**
     * 根据医院ID获取医院设置
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id或者医院设置")
    @GetMapping("getHospSet/{id}")
    public Result getHospSet(@PathVariable Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return Result.ok(hospitalSet);
    }

    /**
     * 修改医院设置
     *
     * @param hospitalSet
     * @return
     */
    @ApiOperation(value = "修改医院设置")
    @PostMapping("updateHospitalSet")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet) {
        boolean flag = hospitalSetService.updateById(hospitalSet);
        if (flag) {
            return Result.ok();
        }
        return Result.fail();
    }

    /**
     * 批量删除医院设置
     *
     * @param idList
     * @return
     */
    @ApiOperation(value = "批量删除医院设置")
    @DeleteMapping("batchRemove")
    public Result batchRemoveHospitalSet(@RequestBody List<Long> idList) {
        hospitalSetService.removeByIds(idList);
        return Result.ok();
    }

    /**
     * 医院设置锁定和解锁
     *
     * @param id
     * @param status
     * @return
     */
    @ApiOperation(value = "医院设置锁定和解锁")
    @PutMapping("lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable Long id,
                                  @PathVariable Integer status) {
        //根据id查询医院设置信息
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        //设置状态
        hospitalSet.setStatus(status);
        //调用方法
        hospitalSetService.updateById(hospitalSet);
        return Result.ok();
    }

    /**
     * 发送签名秘钥
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "发送签名秘钥")
    @PutMapping("sendKey/{id}")
    public Result lockHospitalSet(@PathVariable Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();
        //TODO 发送短信
        return Result.ok();
    }

    /**
     * 统一异常处理
     *
     * @return
     */
    @ApiOperation(value = "统一异常处理")
    @GetMapping("global/exception/handler")
    public Result GlobalExceptionHandler() {
        int i = 1 / 0;
        return Result.ok();
    }

    /**
     *  自定义异常处理
     *
     * @return
     */
    @ApiOperation(value = "自定义异常处理")
    @GetMapping("custom/exception/handler")
    public Result CustomExceptionHandler() {
        try {
            int i = 1 / 0;
        } catch (Exception e) {
            throw new YyghException("失败", 201);
        }
        return Result.ok();
    }
}